package com.jingbit.ai.base;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import android.util.Log;

/**
 * 保存配置辅助类
 * 
 * @author Wellsa
 *
 */
public class BasePreferences {
	private static final String SP_NAME = "sp_default";
	private SharedPreferences sPreferences;
	public BasePreferences() {
		this(null);
	}
	

	public BasePreferences(String fileName) {
		if (TextUtils.isEmpty(fileName)) {
			fileName=SP_NAME;
		}
		sPreferences = BaseApp.application.getSharedPreferences(fileName, Activity.MODE_PRIVATE);
	}

	public boolean getBoolean(String key) {
		return sPreferences.getBoolean(key, false);
	}

	public boolean getBoolean(String key, boolean bool) {
		return sPreferences.getBoolean(key, bool);
	}

	public float getFloat(String key) {
		return sPreferences.getFloat(key, -1);
	}

	public String getString(String key) {
		return sPreferences.getString(key, "");
	}

	public String getString(String key, String value) {
		return sPreferences.getString(key, value);
	}

	public long getLong(String key) {
		return sPreferences.getLong(key, -1);
	}

	public int getInteger(String key) {
		return sPreferences.getInt(key, -1);
	}

	public int getInteger(String key, Integer value) {
		return sPreferences.getInt(key, value);
	}

	public void setPreferences(String key, Object value) {
		Log.d(BaseApp.TAG, "setPreferences："+ key+"---"+value);
		Editor editor = sPreferences.edit();
		if (value instanceof Integer) {
			editor.putInt(key, (Integer) value);
		} else if (value instanceof String) {
			editor.putString(key, (String) value);
		} else if (value instanceof Boolean) {
			editor.putBoolean(key, (Boolean) value);
		} else if (value instanceof Long) {
			editor.putLong(key, (Long) value);
		} else if (value instanceof Float) {
			editor.putFloat(key, (Float) value);
		} else if (value instanceof byte[]) {
			editor.putString(key, value.toString());
		}
		boolean edit = editor.commit();
		Log.d(BaseApp.TAG, "setPreferences："+edit+"---"+ key+"---"+ value);
	}

}
